package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired(required = false)
    private SetmealMapper setmealMapper;
    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;
    @Autowired(required = false)
    private RedisTemplate redisTemplate;

    @Override
    @Transactional
    @CacheEvict(value = "setmeal",allEntries = true)
    public boolean save(SetmealDto setmealDto) {
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealMapper.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSort(0);
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
        }

        return true;
    }

    //查询所有套餐和分类并且分页
    @Override
    public Page<SetmealDto> findAll(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<SetmealDto> setmealDtoList = setmealMapper.findAll(name);
        PageInfo<SetmealDto> pageInfo = new PageInfo<>(setmealDtoList);

        Page<SetmealDto> pageObj = new Page<>();
        pageObj.setRecords(pageInfo.getList());
        pageObj.setTotal(pageInfo.getTotal());
        return pageObj;
    }

    //批量删除
    @Override
    @Transactional
    @CacheEvict(value = "setmeal",allEntries = true)
    public void delete(Long[] ids, Long updateUser) {
        Integer count = setmealMapper.countByIds(ids);
        if (count > 0) {
            throw new BusinessException("不能删除起售的套餐");
        }
        setmealMapper.delete(ids, updateUser, LocalDateTime.now());
        setmealDishMapper.deleteByIds(ids, updateUser, LocalDateTime.now());

    }

    @Override
    @CacheEvict(value = "setmeal",allEntries = true)
    public boolean updateStatus(Long[] ids, Integer status) {
        for (Long id : ids) {
            SetmealDto setmealDto = setmealMapper.findById(id);
            redisTemplate.delete("setmeal_" + setmealDto.getCategoryId() + "_1");
        }
        return setmealMapper.updateStatusById(ids, status) > 0;
    }

//    @Override
//    public boolean updateStatus2(Long[] ids) {
//        return setmealMapper.updateStatusById2(ids)>0;
//    }

    //数据回显
    @Override
    public SetmealDto findById(Long id) {
        return setmealMapper.findById(id);
    }

    //修改
    @Override
    @Transactional
    @CacheEvict(value = "setmeal",allEntries = true)
    public void updateSetmealAndSetmealDish(SetmealDto setmealDto) {
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setStatus(1);
        setmealMapper.updateById(setmealDto);
        setmealDishMapper.deleteById(setmealDto.getId());
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setSort(0);
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
        }
        setmealDishMapper.saveSetmealDish(setmealDishes);
    }

    @Override
    @Cacheable(value = "setmeal",key = "#categoryId+'_'+#status")
    public List<Setmeal> findByCategoryIdAndStatus(Long categoryId, Integer status) {
        String key = "setmeal_" + categoryId + "_" + status;
        List<Setmeal> setmealList = (List<Setmeal>) redisTemplate.opsForValue().get(key);
        if (setmealList == null) {
            setmealList = setmealMapper.findByCategoryIdAndStatus(categoryId, status);
            redisTemplate.opsForValue().set(key,setmealList);
        }
        return setmealList;
    }
}
